package com.yunshu.yunshuapp.xianhuo;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.fasterxml.jackson.databind.JsonNode;
import com.yunshu.yunshuapp.NavigationBar;
import com.yunshu.yunshuapp.R;
import com.yunshu.yunshuapp.kuaixun.KuaixunChannel;
import com.yunshu.yunshuapp.network.YSCallback;
import com.yunshu.yunshuapp.network.YSRequest;
import com.yunshu.yunshuapp.utils.YSUtils;

public class XianhuoTemplateActivity extends MyPostsActivity implements  XianhuoClickListener {

    public static final int REQUEST_CODE = 10003;
    private boolean selectionMode = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        NavigationBar bar = (NavigationBar) findViewById(R.id.navigationBar);
        bar.setTitle("模板管理");
        bar.putInActivity(this);

        this.adapter.onClickListener = this;
        this.adapter.shouldShowAudit = false;

        if(getIntent().getStringExtra("selectionMode")!=null){
            selectionMode = true;
        }
    }

    @Override
    public void onClick(final Xianhuo model) {
        if (selectionMode){
            Intent mIntent = new Intent();

            mIntent.putExtra("data", model.serializeToString());

            this.setResult(RESULT_OK,mIntent);
            this.finish();
        }else{
            new AlertDialog.Builder(this).setTitle("提示")//设置对话框标题

                    .setMessage("要删除此模板吗？")//设置显示的内容 ;
                    .setPositiveButton("删除",new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                            deleteTemplate(model);
                        }
                    }).setNegativeButton("我再想想",null).show();
        }
    }

    private void deleteTemplate(Xianhuo model) {


        new YSRequest(this).GET(YSRequest.XIANHUO_TEMPLATE_DELETE, "id="+model.getXianhuoId(), new YSCallback() {
            @Override
            public void onResponse(int isSuccess, JsonNode result) {
                if (isSuccess == YSRequest.REQUEST_OK){
                    refresh();
                    runOnUiThread(new Runnable() {      // UI thread
                        @Override
                        public void run() {
                            YSUtils.showToastOn(XianhuoTemplateActivity.this,"成功删除！");
                        }
                    });
                }
            }
        });

    }

    public String getURL() {
        return YSRequest.XIANHUO_TEMPLATE_LIST;
    }
}
